<template>
  <div class="goods-list">
    <el-card class="card-one">
        <el-form :inline="true" class="from">
            <el-form-item label="商品名称">
                <el-input v-model="searchForm.searchText" placeholder="请输入商品名称" clearable/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleSearch()">
                    搜索
                </el-button>
                <el-button>
                    重置
                </el-button>

                <el-button @click="handleAdd()">
                    新增商品
                </el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card>
        <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width:100%">
        <el-table-column type="index" label="商品id" width="60" align="center" />
        <el-table-column prop="name" label="商品名称" min-width="200" />
        <el-table-column label="商品图片" width="100" >
            <template #default="{ row }">
                <el-image
                :src="'http://localhost:9191'+row.imgs"
                :preview-src-list="['http://localhost:9191'+row.imgs]"
                fit="cover"
                style="width:50px;height:50px"
                />
            </template>
        </el-table-column>
        <el-table-column prop="description" label="商品描述" min-width="180" show-overflow-tooltip />
        <el-table-column prop="discount" label="折扣" min-width="180" />
        <el-table-column prop="sales" label="销量" min-width="180" />
        <el-table-column label="销售额（元）" min-width="180" >
            <template #default="{row}">
                ${{row.price.toFixed(2)}}
            </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="180" />
        <el-table-column label="推荐">
            <template #default="{row}">
                <el-switch
                v-model="row.recommend"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                />
            </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180" align="center">
            <template #default="{ row }">
                <el-button @click="handleAmend(row)">修改</el-button>
                <el-button>删除</el-button>
            </template>
        </el-table-column>
        </el-table>

        <div class="pagination">
            <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[5, 10, 15, 20]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            />
        </div>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
import request from '@/utils/request'
import router from '@/router'

const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(5)
const searchForm = reactive({
    searchText:'',
    categoryId:'',
    minPrice:null,
    maxPrice:null
})

// 获取商品列表
const getGoodsList = async () => {
  loading.value = true
  try {
    const res = await request({
      url: '/api/good/fullPage',
      method: 'get',
      params: {
          pageNum: currentPage.value,
          pageSize: pageSize.value,
        ...searchForm
      }
    })
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error('获取商品列表失败:', error)
  } finally {
    loading.value = false
  }
}

//搜索
const handleSearch = ()=>{
    currentPage.value=1,
    getGoodsList()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getGoodsList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getGoodsList()
}

//新增商品
const handleAdd=()=>{
    router.push('/admin/index/goods/add')
}

//修改商品
const handleAmend = (row) => {
    router.push(`/admin/index/goods/amend/${row.id}`)
}
onMounted(()=>{
    getGoodsList()
})
</script>

<style scoped>
.from{
    display: flex;
    flex-direction: column;
}
</style>
